package com.eight.cloud.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import com.eight.cloud.admin.model.dto.AppDto;
import com.eight.cloud.admin.service.IAppService;
import com.eight.cloud.core.global.model.AppUser;
import com.eight.cloud.core.global.result.Result;
import com.eight.cloud.openfeign.admin.vo.AppVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* app信息表 管理接口
*
* @ClassName: AppController
* @Author TXC
* @Date 2025-01-07 17:34
*/
@RestController
@RequestMapping("/app")
public class AppController {
    @Resource
    private IAppService appService;
    /**
     * 创建app
     * @return @Link Result
     */
    @PostMapping("/create")
    public Result<AppVo> settled(@RequestBody AppDto appDto) {
        AppVo app = appService.create(appDto);
        if (ObjectUtil.isNull(app)) {
            return Result.fail("入驻失败");
        }
        return Result.ok(app);
    }

    /**
     * 编辑
     * @param appDto app信息
     * @return @Link Result
     */
    @PutMapping("/edit")
    public Result<?> edit(@RequestParam AppDto appDto) {
        boolean flag = appService.edit(appDto);
        if (!flag) {
            return Result.fail("编辑失败");
        }
        return Result.ok();
    }

    /**
     * 删除
     * @param uuid appid
     * @return @Link Result
     */
    @DeleteMapping("/{uuid}")
    public Result<?> delete(@PathVariable String uuid) {
        appService.removeByUuid(uuid);
        return Result.ok();
    }

    /**
     * 查询租户详情
     * @param uuid appid
     * @return @Link Result
     */
    @GetMapping("/{uuid}")
    public Result<AppVo> query(@PathVariable String uuid) {
        return Result.ok(appService.detail(uuid));
    }

    /**
     * 获取密钥
     * @param secretId secretId
     * @return @Link Result
     */
    @GetMapping("/querySecret")
    public Result<AppUser> queryAppBySecretId(@RequestParam String secretId) {
        return Result.ok(appService.getAppBySecretId(secretId));
    }

    /**
     * 获取密钥
     * @param appid appid
     * @return @Link Result
     */
    @GetMapping("/queryAppid")
    public Result<AppUser> queryAppByUuid(@RequestParam String appid) {
        return Result.ok(appService.getAppByUuid(appid));
    }

    /**
     * 更新密钥
     * @param uuid appid
     * @return
     */
    @GetMapping("/updateSecretKey/{uuid}")
    public Result<?> updateSecretKey(@PathVariable String uuid) {
        return Result.ok(appService.updateSecretKey(uuid));
    }

}
